package energyware.graphs;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import energyware.gui.R;
import energyware.utils.ProtocolSeparator;

public class LineGraphActivity extends Activity {

	private GraphicalView mChartView;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		Bundle extras=getIntent().getExtras();
		String graphData = extras.getString("graphData");

		graphData = graphData.substring(1);
		String estado_0 = "";
		String estado_1 = "";
		String estado_2 = "";
		if(graphData.split(ProtocolSeparator.RESPONSES).length>0){
			estado_0 = graphData.split(ProtocolSeparator.RESPONSES)[0];
		}
		if(graphData.split(ProtocolSeparator.RESPONSES).length>1){
			estado_1 = graphData.split(ProtocolSeparator.RESPONSES)[1];
		}
		if(graphData.split(ProtocolSeparator.RESPONSES).length>2){
			estado_2 = graphData.split(ProtocolSeparator.RESPONSES)[2];
		}

		int [] x_0 = new int[estado_0.split(ProtocolSeparator.ROWS).length];
		int [] y_0 = new int[estado_0.split(ProtocolSeparator.ROWS).length];


		if(!estado_0.equals("")){
			String paquete_0 = "";
			for(int i = 0;i<estado_0.split(ProtocolSeparator.ROWS).length;i++){
				paquete_0 = estado_0.split(ProtocolSeparator.ROWS)[i];
				x_0[i] = Integer.parseInt(paquete_0.split(ProtocolSeparator.COLUMNS)[2]);
				y_0[i] = Integer.parseInt(paquete_0.split(ProtocolSeparator.COLUMNS)[1]);
			}
		}

		int [] x_1 = new int[estado_1.split(ProtocolSeparator.ROWS).length];
		int [] y_1 = new int[estado_1.split(ProtocolSeparator.ROWS).length];


		if(!estado_1.equals("")){
			String paquete_1 = "";
			for(int i = 0;i<estado_1.split(ProtocolSeparator.ROWS).length;i++){
				paquete_1 = estado_1.split(ProtocolSeparator.ROWS)[i];
				x_1[i] = Integer.parseInt(paquete_1.split(ProtocolSeparator.COLUMNS)[2]);
				y_1[i] = Integer.parseInt(paquete_1.split(ProtocolSeparator.COLUMNS)[1]);
			}
		}

		int [] x_2 =  new int[estado_2.split(ProtocolSeparator.ROWS).length];
		int [] y_2 =  new int[estado_2.split(ProtocolSeparator.ROWS).length];

		if(!estado_2.equals("")){
			String paquete_2 = "";
			for(int i = 0;i<estado_2.split(ProtocolSeparator.ROWS).length;i++){
				paquete_2 = estado_2.split(ProtocolSeparator.ROWS)[i];
				x_2[i] = Integer.parseInt(paquete_2.split(ProtocolSeparator.COLUMNS)[2]);
				y_2[i] = Integer.parseInt(paquete_2.split(ProtocolSeparator.COLUMNS)[1]);
			}
		}


		TimeSeries series_0 = new TimeSeries(getString(R.string.state0));
		TimeSeries series_1 = new TimeSeries(getString(R.string.state1));
		TimeSeries series_2 = new TimeSeries(getString(R.string.state2));
		for (int i=0;i<x_0.length;i++){
			series_0.add(x_0[i], y_0[i]);
		}

		for (int i=0;i<x_1.length;i++){
			series_1.add(x_1[i], y_1[i]);
		}

		for (int i=0;i<x_2.length;i++){
			series_2.add(x_2[i], y_2[i]);
		}


		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		dataset.addSeries(series_0);
		dataset.addSeries(series_1);	
		dataset.addSeries(series_2);

		XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
		XYSeriesRenderer renderer_0 = new XYSeriesRenderer();
		XYSeriesRenderer renderer_1 = new XYSeriesRenderer();
		XYSeriesRenderer renderer_2 = new XYSeriesRenderer();

		renderer_0.setColor(Color.rgb(122, 179, 23));
		renderer_1.setColor(Color.RED);
		renderer_2.setColor(Color.BLUE);

		mRenderer.addSeriesRenderer(renderer_0);
		mRenderer.addSeriesRenderer(renderer_1);
		mRenderer.addSeriesRenderer(renderer_2);
		mRenderer.getSeriesRendererAt(0).setDisplayChartValues(true);
		mRenderer.getSeriesRendererAt(1).setDisplayChartValues(true);
		mRenderer.getSeriesRendererAt(2).setDisplayChartValues(true);
		mRenderer.setApplyBackgroundColor(true);
		mRenderer.setMarginsColor(Color.WHITE);
		mRenderer.setBackgroundColor(Color.WHITE);
		mRenderer.setAxesColor(Color.BLACK);
		mRenderer.setLabelsTextSize(20);
		mRenderer.setXLabelsColor(Color.BLACK);
		mRenderer.setYLabelsColor(0, Color.BLACK);
		mRenderer.setLabelsColor(Color.BLACK);
		mRenderer.setShowGrid(true);
		mRenderer.setGridColor(Color.GRAY);
		mRenderer.setBarSpacing(0.5f);
		mRenderer.setYTitle(getString(R.string.Number_Of_states));
		mRenderer.setXTitle(getString(R.string.hour));


		if (mChartView == null) {
			Log.d("Oncreate ", "if (mChartView == null)");
			mChartView = ChartFactory.getBarChartView(getApplicationContext(), dataset, mRenderer, Type.DEFAULT);
			setContentView(mChartView);

		} else {
			//          mChartView.repaint();
			Log.d("Oncreate ", "if (mChartView != null)");
		}

	}

}
